package com.hoshiicloud.saleorder.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.saleorder.entity.CouponList;
import com.hoshiicloud.saleorder.param.CouponListAddParam;
import com.hoshiicloud.saleorder.param.CouponListQueryParam;
import com.hoshiicloud.saleorder.param.CouponListUpdateParam;
import com.hoshiicloud.saleorder.service.CouponListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import javax.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 优惠抵现单 控制器
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-06-03
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/saleorder/coupon-list")
@Api(value = "销售订单", tags = {"优惠抵现单"})
public class CouponListController {

    @Autowired
    private CouponListService couponListService;

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 分页查询优惠抵现单
     */
    @ApiOperation(value = "分页查询优惠抵现单")
    @PostMapping("/query/page")
    public Response page(@RequestBody @Valid CouponListQueryParam param) {
        try {
            log.info("分页查询优惠抵现单");
            QueryWrapper<CouponList> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(param.getSubId() != null, CouponList::getSubId, param.getSubId())
                    .eq(StringUtils.isNotBlank(param.getPayOrderNo()), CouponList::getPayOrderNo, param.getPayOrderNo())
                    .eq(param.getPayTime() != null, CouponList::getPayTime, param.getPayTime())
                    .eq(param.getAmount() != null, CouponList::getAmount, param.getAmount())
                    .eq(param.getPayType() != null, CouponList::getPayType, param.getPayType())
                    .eq(param.getStatus() != null, CouponList::getStatus, param.getStatus())
                    .eq(StringUtils.isNotBlank(param.getRefNo()), CouponList::getRefNo, param.getRefNo())
                    .eq(true, CouponList::getDeleted, param.getDeleted() == null ? 0 : param.getDeleted());
            IPage<CouponList> page = new Page<>(Long.parseLong(param.getPageNumber()),
                    Long.parseLong(param.getPageSize()));
            String key = "CouponListQueryParam{" + param.toString() + "} ,Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            return Response.data(couponListService.page(page, queryWrapper, key));
        } catch (Exception e) {
            log.error("分页查询优惠抵现单异常", e);
            throw new ServiceException("分页查询优惠抵现单异常");
        }
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询优惠抵现单
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询优惠抵现单")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(couponListService.getById(id));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增优惠抵现单")
    public Response save(@RequestBody @Valid CouponListAddParam param) {

        CouponList entity;

        try {
            entity = BeanUtils.covertType(param, CouponList.class);
        } catch (Exception e) {
            log.error("类型转换异常{}", e.getMessage());
            throw new ServiceException("新增优惠抵现单异常");
        }

        return Response.data(couponListService.save(entity));

    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改优惠抵现单
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改优惠抵现单")
    public Response update(@RequestBody @Valid CouponListUpdateParam param) {

        CouponList entity;

        try {
            entity = BeanUtils.covertType(param, CouponList.class);
        } catch (Exception e) {
            log.error("修改优惠抵现单类型转换异常{}", e.getMessage());
            throw new ServiceException("修改优惠抵现单异常");
        }

        return Response.data(couponListService.updateById(entity));

    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 删除优惠抵现单
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除优惠抵现单")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response delete(@PathVariable("id") Long id) {
        return Response.data(couponListService.deleteById(id));
    }

    /**
     * @Author:campchen
     * @Description: StringUtils.join(ids, " ") 为缓存中添加唯一KEY  如不合适可选择更好方法
     * @Date:上午 10:18 2019/5/11
     * @Param: ids
     * @return: com.hoshiicloud.common.rpc.Response
     */
    @ApiOperation(value = "根据id集合查询优惠抵现单")
    @GetMapping("/getByIds")
    public Response getByIds(@RequestParam List<Long> ids) {
        if (CollectionUtils.isNotEmpty(ids)) {
            ids.sort(null);
            return Response.data(couponListService.getByIds(ids, StringUtils.join(ids, "")));
        } else {
            return Response.data(null);
        }
    }
}
